package com.kws.merchant.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.kws.common.config.constant.MinioCommonBucket;
import com.kws.common.config.util.DateTimeUtil;
import com.kws.merchant.config.MinioAPI;
import com.kws.merchant.domain.OperationTicketFile;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.kws.merchant.domain.po.TickFilePo;
import com.kws.merchant.mapper.OperationTicketFileMapper;
import com.kws.merchant.service.MerchantService;
import com.kws.merchant.service.OperationTicketFileService;
import com.kws.merchant.util.FileUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.time.LocalDate;
import java.util.List;

/**
 * 运维-两票管理模板(OperationTicketFile)表服务实现类
 *
 * @author yxy
 * @since 2024-10-28 10:06:15
 */
@Service("operationTicketFileService")
public class OperationTicketFileServiceImpl extends ServiceImpl<OperationTicketFileMapper, OperationTicketFile> implements OperationTicketFileService {

    @Resource
    private OperationTicketFileMapper operationTicketFileMapper;
    @Autowired
    private MerchantService merchantService;
    /**
     * 分页查询列表
     */
    @Override
    public Page<OperationTicketFile> pageList(OperationTicketFile operationTicketFile) {
        Page<OperationTicketFile> page = new Page<>(0, 10);
        LambdaQueryWrapper<OperationTicketFile> queryWrapper = Wrappers.lambdaQuery();

        return this.page(page, queryWrapper);
    }

    /**
     * 两票模板上传
     * @return
     */
    @Override
    public Object upload(TickFilePo tickFilePo) {
        Integer type = tickFilePo.getType();
        Integer tickType = tickFilePo.getTickType();
        MultipartFile file = tickFilePo.getFile();
        LambdaQueryWrapper<OperationTicketFile> wrapper = Wrappers.<OperationTicketFile>lambdaQuery().eq(OperationTicketFile::getType, type);
        if (type==0){
            wrapper.eq(OperationTicketFile::getWorkType,tickType);
        }else{
            wrapper.eq(OperationTicketFile::getHandleType,tickType);
        }
        OperationTicketFile ticketFile = this.getOne(wrapper);
        if (ObjectUtils.isNotEmpty(ticketFile)){
            throw new RuntimeException("该票类型模板已上传！！");
        }
        ticketFile=new OperationTicketFile();
        ticketFile.setType(type);
        if (type==0){
            ticketFile.setWorkType(tickType);
        }else {
            ticketFile.setHandleType(tickType);
        }
        String filePath = merchantService.uploadCompanyFile(file);
        ticketFile.setFile(FileUtil.subFileName(filePath));
        ticketFile.setName(file.getOriginalFilename());
        return this.save(ticketFile);
    }
    /**
     * 两票模板列表
     * @param type
     * @return
     */
    @Override
    public List<OperationTicketFile> listByType(Integer type) {
        List<OperationTicketFile> list = this.list(Wrappers.<OperationTicketFile>lambdaQuery().eq(OperationTicketFile::getType,type));
        list.forEach(x->{
            x.setFile(FileUtil.fixFileName()+x.getFile());
        });
        return list;
    }


}
